home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / mobile / fma-2.0-stable-setup.exe / {app} / source / uWaitComplete.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-14  |  6.1 KB  |  228 lines

  1. unit uWaitComplete;
  2.  
  3. {
  4. *******************************************************************************
  5. * Descriptions: Sony Ericsson progress bar
  6. * $Source: /cvsroot/fma/fma/uWaitComplete.pas,v $
  7. * $Locker:  $
  8. *
  9. * Todo:
  10. *    - remove ErrorOccured, use DebugStr only as error flag.
  11. *
  12. * Change Log:
  13. * $Log: uWaitComplete.pas,v $
  14. * Revision 1.13.6.1  2004/10/14 16:43:28  z_stoichev
  15. * Bugfixes
  16. *
  17. * Revision 1.13  2004/07/02 20:12:15  z_stoichev
  18. * no message
  19. *
  20. * Revision 1.12  2004/07/02 18:14:17  lordlarry
  21. * Fixed 100% CPU when communicating
  22. *
  23. * Revision 1.11  2004/06/28 22:42:26  z_stoichev
  24. * Possible freeze fixed
  25. *
  26. * Revision 1.10  2004/06/28 09:12:38  z_stoichev
  27. * Bugfixes
  28. *
  29. * Revision 1.9  2004/06/25 08:11:25  z_stoichev
  30. * Added Message storage is 90% full warning.
  31. *
  32. * Revision 1.8  2004/05/19 18:34:16  z_stoichev
  33. * Build 0.1.0.35c
  34. *
  35. * Revision 1.7  2004/03/11 13:38:16  z_stoichev
  36. * Show user friendly message on AT error.
  37. *
  38. * Revision 1.6  2004/03/08 09:57:54  z_stoichev
  39. * Fixed timeout on long operations.
  40. *
  41. * Revision 1.5  2004/02/03 16:29:03  z_stoichev
  42. * Bugfixes.
  43. *
  44. * Revision 1.4  2004/01/26 10:32:12  z_stoichev
  45. * Added uWaitComplete again.
  46. *
  47. * Revision 1.2  2003/12/04 16:22:33  z_stoichev
  48. * Bugfixes
  49. *
  50. *
  51. }
  52.  
  53. interface
  54.  
  55. uses
  56.   Forms, Windows, Classes, Controls, SysUtils;
  57.  
  58. type
  59.   TWaitThread = class(TThread)
  60.   private
  61.     { Private declarations }
  62.     DebugStr: String;
  63.     FIsFinished: Boolean;
  64.     FIsStarted: Boolean;
  65.     procedure DoDebug;
  66.     procedure ShowDebug(str: String);
  67.     function Get_IsErrorOccur: Boolean;
  68.   protected
  69.     TxData,RcWait: String;
  70.     ErrorOccured: Boolean;
  71.     procedure Execute; override;
  72.   public
  73.     constructor Create(SendData,WaitFor: string);
  74.     function GetLastError: string;
  75.   published
  76.     property Started: Boolean read FIsStarted;
  77.     property Finished: Boolean read FIsFinished;
  78.     property IsErrorOccur: Boolean read Get_IsErrorOccur;
  79.   end;
  80.  
  81. implementation
  82.  
  83. uses
  84.   Unit1, gsm_sms;
  85.  
  86. { Important: Methods and properties of objects in VCL or CLX can only be used
  87.   in a method called using Synchronize, for example,
  88.  
  89.       Synchronize(UpdateCaption);
  90.  
  91.   and UpdateCaption could look like,
  92.  
  93.     procedure TWaitThread.UpdateCaption;
  94.     begin
  95.       Form1.Caption := 'Updated in a thread';
  96.     end; }
  97.  
  98. { TWaitThread }
  99.  
  100. constructor TWaitThread.Create(SendData, WaitFor: string);
  101. begin
  102.   TxData := SendData;
  103.   RcWait := WaitFor;
  104.   FIsFinished := False;
  105.   inherited Create(False);
  106. end;
  107.  
  108. procedure TWaitThread.DoDebug;
  109. begin
  110.   if ErrorOccured then
  111.     Form1.Debug('ERROR: '+DebugStr)
  112.   else begin
  113.     Form1.Debug('[TX] '+DebugStr);
  114.     DebugStr := '';
  115.   end;
  116. end;
  117.  
  118. procedure TWaitThread.Execute;
  119. begin
  120.   ReturnValue := 0;
  121.   ErrorOccured := False;
  122.   { Wait for any previous thread to finish }
  123.   repeat
  124.     if not Form1.FWaitingOK and //not Form1.FScriptRunning and
  125.        (WaitForSingleObject(Form1.FWaitCompleteIsBusyEvent,50) = WAIT_OBJECT_0) then break;
  126.     if Form1.FAbort or Application.Terminated or Terminated then begin
  127.       Form1.ActiveThread := nil;
  128.       FIsFinished := True;
  129.       FIsStarted := True;
  130.       exit;
  131.     end;
  132.   until False;
  133.   { Ok, continue }
  134.   FIsStarted := True;
  135.   Screen.Cursor := crAppStart;
  136.   with Form1 do try
  137.     FBusy := True;
  138.     ActiveThread := Self;
  139.     FWaitStr := RcWait;
  140.     FLastCommand := TxData;
  141.     ResetEvent(FWaitCompleteEvent);
  142.     if TxData <> '' then begin
  143.       if (TxData = 'AT*EOBEX') and (FWaitStr = 'CONNECT') then begin
  144.         FAlreadyInUseObex := False;
  145.         FObexConnecting := True;
  146.       end;
  147.       if Pos('AT+CPMS="',TxData) = 1 then
  148.         Form1.FLastMessageStore := Copy(TxData,10,2); // ME or SM or...
  149.       { Convert data }
  150.       ShowDebug(TxData);
  151.       if FDoCharConvertion then begin
  152.         TxData := ConvertCharSet(TxData, True);
  153.       end;
  154.       { Where and when to clear FRxBuffer ? }
  155.       if not FWaitingOK then
  156.         FRxBuffer.Clear;
  157.       //ShowDebug('RxBuffer: '+IntToStr(FRxBuffer.Count)+' line(s) so far');
  158.       FWaitingOK := RcWait = 'OK';
  159.       { Send data... }
  160.       FMSec := GetTickCount + FInactivityTimeout;
  161.       if FConnectionType = 0 then WBtSocket.SendStr(TxData + #13)
  162.       else if FConnectionType = 1 then WIrSocket.SendStr(TxData + #13)
  163.       else ComPort.WriteStr(TxData + #13); // Serial
  164.     end
  165.     else
  166.       FMSec := GetTickCount + 500;
  167.     { Wait complete }
  168.     FTimedout := False;
  169.     while (WaitForSingleObject(FWaitCompleteEvent, 200) = WAIT_TIMEOUT) and not FAbort and not FTimedout do begin
  170.       FTimedout := not (GetTickCount < FMSec);
  171.       if Application.Terminated then FAbort := True;
  172.     end;
  173.     if FWaitStr = 'ERROR' then begin
  174.       ErrorOccured := True;
  175.       ShowDebug('Command return error or not understood ('+TxData+')');
  176.       FWaitStr := '';
  177.     end;
  178.     { Do not error or timeout on stray response check }
  179.     if TxData = '' then begin
  180.       FTimedout := False;
  181.       ErrorOccured := False;
  182.     end;
  183.     { Check for timeout }
  184.     if not FAlreadyInUseObex and FTimedout then begin
  185.       ErrorOccured := True;
  186.       if not IsAutoConnect then ShowDebug('Wait timeout')
  187.         else DebugStr := 'Wait timeout'; // be silent in re-connect mode
  188.     end;
  189.     { Check for user abort }
  190.     if FAbort then begin
  191.       FAbortDetected := True;
  192.       FAbort := False;
  193.       ErrorOccured := True;
  194.       ShowDebug('Aborted by user');
  195.     end
  196.     else
  197.       FAbortDetected := False;
  198.   finally
  199.     { Ok, clear the semafor, allow next commands }
  200.     Screen.Cursor := crDefault;
  201.     ReturnValue := byte(ErrorOccured);
  202.     ActiveThread := nil;
  203.     FIsFinished := True;
  204.     FBusy := False;
  205.     ReleaseSemaphore(FWaitCompleteIsBusyEvent,1,nil);
  206.   end;
  207. end;
  208.  
  209. function TWaitThread.GetLastError: string;
  210. begin
  211.   Result := DebugStr;
  212. end;
  213.  
  214. function TWaitThread.Get_IsErrorOccur: Boolean;
  215. begin
  216.   Result := ErrorOccured and (GetLastError <> '');
  217. end;
  218.  
  219. procedure TWaitThread.ShowDebug(str: String);
  220. begin
  221.   DebugStr := str;
  222.   { Synchronize hangs up when caling fma.function from am.function,
  223.     to avoid such issues, use only DoDebug; (not recommended) }
  224.   Synchronize(DoDebug);
  225. end;
  226.  
  227. end.
  228.